<?php
namespace lib\Storage;

class Local {
	var $path = null;
	function __construct($filepath = null) {
		if($filepath && is_dir($filepath)){
			$this->path = $filepath;
		}else{
			$this->path = APP_DIR.'/upload/';
		}
		if(!is_dir($this->path)){
			$this->set_dir($this->path);
			// mkdir($this->path);
		}
		return true;
	}
	function exists($name) {
		return file_exists($this->path.$name);
	}
	function get($name) {
		return file_get_contents($this->path.$name);
	}
	function downfile($name) {
		readfile($this->path.$name);
		return true;
	}
	function upload($name,$tmpfile) {
		return move_uploaded_file($tmpfile,$this->path.$name);
	}
	function savefile($name,$tmpfile) {
		return rename($tmpfile,$this->path.$name);
	}
	function getsize($name) {
		return filesize($this->path.$name);
	}
	function gettype($name) {
		if(function_exists("finfo_open")){
			$finfo = finfo_open(FILEINFO_MIME_TYPE);
			$type = finfo_file($finfo, $this->path.$name);
			finfo_close($finfo);
			return $type;
		} else {return null;}
	}
	function delete($name) {
		return unlink($this->path.$name);
	}
	function set_dir($dir = null){
		$dir = explode('/', $dir);
		foreach($dir as $v){
			if($v){
				$d .= $v . '/';
				if(!is_dir($d)){
					$state = mkdir($d, 0777);
					// if(!$state)
					// 	$this->errmsg = '在创建目录<font color=red>' . $d . '时出错！';
				}
			}
		}
		return true;
	}
}